package com.yzc.mihuyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzc.mihuyou.entity.Attention;
import com.yzc.mihuyou.entity.User;
import com.yzc.mihuyou.mapper.AttentionMapper;
import com.yzc.mihuyou.service.AttentionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭权文
 * @since 2022-05-06
 */
@Service
public class AttentionServiceImpl extends ServiceImpl<AttentionMapper, Attention> implements AttentionService {
    @Autowired
    AttentionMapper attentionMapper;


    @Override
    public List<User> getAttention (Long id){
        List<User> list = attentionMapper.getAttention(id);
        System.out.print(list);
        return list;
    }

    @Override
    public List<User> getFans (Long id){
        System.out.println(id);
        List<User> list = attentionMapper.getFans(id);
        for (User user:list){
            Attention attention=new Attention();
            attention.setUid(id);
            attention.setAid(user.getUserId());
            user.setPoe(this.relation(attention));
        }
        return list;
    }

    @Override
    public boolean relation(Attention attention) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("Uid",attention.getUid());
        map.put("Aid",attention.getAid());
        map.put("state",1);
        List<Attention> lists=attentionMapper.selectByMap(map);
        return lists.size()>0? true:false;
    }

    @Override
    public JsonResult addAttention(Attention attention) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("Uid",attention.getUid());
        map.put("Aid",attention.getAid());
        List<Attention> lists=attentionMapper.selectByMap(map);
        int i=0;
        if(lists.size()>0){

            if(lists.get(0).getState()==true){
                return  new JsonResult(401,"请勿重复关注");
            }else{
                lists.get(0).setState(true);
                i = attentionMapper.updateById(lists.get(0));
            }

        }else{
            i = attentionMapper.insert(attention);
        }

        if(i>0){
            return new JsonResult(200,"关注成功");
        }
        return new JsonResult(500,"出现异常");
    }

    @Override
    public JsonResult delAttention(Attention attention) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("Uid",attention.getUid());
        map.put("Aid",attention.getAid());
        List<Attention> lists=attentionMapper.selectByMap(map);
        if(lists.size()>0){
            if(lists.get(0).getState()==false){
                return  new JsonResult(401,"请勿重复取消关注");
            }else{
                lists.get(0).setState(false);
                int i = attentionMapper.updateById(lists.get(0));
                if(i>0){
                    return new JsonResult(200,"取消关注");
                }
            }
        }else {
            return new JsonResult(403,"无效取消");
        }
        return new JsonResult(500,"出现异常");

    }

    @Override
    public long allFans(long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Uid",id);
        wrapper.eq("state",1);
        return attentionMapper.selectCount(wrapper);
    }

    @Override
    public long allAttention(long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Aid",id);
        wrapper.eq("state",1);
        return attentionMapper.selectCount(wrapper);
    }


}
